package com.tianfeng.fenghuotoutiao.ui.fragment.base;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;
import com.tianfeng.fenghuotoutiao.R;
import com.tianfeng.fenghuotoutiao.utils.photo.TileDrawable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class PicDuoDianchukongActivity extends AppCompatActivity {
    private TileDrawable mTileDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic_duo_dianchukong);
        String url=getIntent().getStringExtra("url");
        Toast.makeText(this, "温馨提示:长按保存图片或者单击图片关闭", Toast.LENGTH_LONG).show();
        final PhotoView pinchImageView = findViewById(R.id.photo_view);
        pinchImageView.setBackgroundColor(getResources().getColor(R.color.text_black));
        Glide.with(this)
                .load(url)
                .into(pinchImageView);

             pinchImageView.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     finish();
                 }
             });
             pinchImageView.setOnLongClickListener(new View.OnLongClickListener() {
                 @Override
                 public boolean onLongClick(View v) {
                     new Thread(new Runnable() {
                         @Override
                         public void run() {
                             LoadImage2(url);
                         }
                     }).start();

                     return false;
                 }
             });
    }
    private Bitmap mBitmap;
    private String mFileName = "test.jpg";

    /**
     * 下载图片从输入里面
     */
    private void LoadImage2(String url1) {
        try {
            URL url = new URL(url1);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5 * 1000);
            conn.setRequestMethod("GET");
            InputStream inputStream = conn.getInputStream();
            mBitmap = BitmapFactory.decodeStream(inputStream);
            Message message = new Message();
            message.what = 1;
            connectHandler.sendMessage(message);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 主线程更新UI
     */
    private Handler connectHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:

                    saveBitmap(mBitmap);

                    break;

            }

        }
    };
    String bitmapName = "fenxiang.jpg";
    File file1;
    String QQFilePath;
    public void saveBitmap(Bitmap bmp) {

        try { // 获取SDCard指定目录下
            String sdCardDir = Environment.getExternalStorageDirectory() + "/zupubao/";
            File dirFile = new File(sdCardDir);  //目录转化成文件夹
            if (!dirFile.exists()) {              //如果不存在，那就建立这个文件夹
                dirFile.mkdirs();
            }                          //文件夹有啦，就可以保存图片啦
            File file1 = new File(sdCardDir, bitmapName);// 在SDcard的目录下创建图片文,以当前时间为其命名
            FileOutputStream out = new FileOutputStream(file1);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
            Log.e("saveBitMap", "saveBitmap: 图片保存到" + Environment.getExternalStorageDirectory() + "/zupubao/" + bitmapName);
            QQFilePath = Environment.getExternalStorageDirectory() + "/zupubao/" + "fenxiang.jpg";
//            showShare(QQFilePath);
            MediaStore.Images.Media.insertImage(getContentResolver(), QQFilePath, "title", "description");
            out.flush();
            out.close();
            finish();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        Toast.makeText(HahItemActivity.this,"保存已经至"+Environment.getExternalStorageDirectory()+"/CoolImage/"+"目录文件夹下", Toast.LENGTH_SHORT).show();
    }

}


